package dbutils;

import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/7/1 16:38
 * @since JDK 1.8
 *
 * sql创建工具
 */
public class SqlCreateUtils {

    public static void main(String[] args) {
        String s=pageSqlCreate("table",1,10);
        System.out.println(s);
    }

    /**
     * 生成分页查询的sql
     * @param tableName
     * @param page
     * @param size
     * @return
     */
    public static String pageSqlCreate(String tableName,Integer page,Integer size){
        Integer page1=(page-1)*size;
        String str=String.format("select * from %s limit %s,%s",tableName,page1,size);
        return str;
    }


    /**
     * 生成删除的sql
     * @param tableName  表明
     * @param mapList  数据集
     * @param key  主键
     * @return 删除的sql
     */
    public static String deleteListSql(String tableName, List<Map<String, Object>> mapList, String key){
        return deleteListSql(tableName,mapList,null,key);
    }

    public static String deleteListSql(String tableName, List<Map<String, Object>> mapList, String mapkey,String key){
        StringJoiner stringJoiner=new StringJoiner(";");
        for (int i = 0; i < mapList.size(); i++) {
            Map<String, Object> map=mapList.get(i);
            String str;
            if(mapkey!=null){
               str=deleteSqlCreate(tableName,key,map.get(mapkey));
            }else {
                str=deleteSqlCreate(tableName,key,map.get(key));
            }

            stringJoiner.add(str);
        }
        return stringJoiner.toString();
    }

    private static String deleteSqlCreate(String tableName,String key,Object value){
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("delete ").append(tableName)
                .append(" where ").append(key).append(" ='")
                .append(value).append("'");
        return stringBuffer.toString();
    }


}
